package it.uniroma2;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.uncommons.maths.random.ExponentialGenerator;
import org.uncommons.maths.random.MersenneTwisterRNG;

public class ExponentialDistribution {

	private static final long ONE_SECOND = 1000;
	private ExponentialGenerator gen;
	private Executor executor;
	private long millisecPerClass;
	
	
	public ExponentialDistribution(double requestPerSecond, Executor executor, long minutesPerClass) {
		
		this.millisecPerClass = 1000 * 60 * minutesPerClass;

		Random rng = new MersenneTwisterRNG();
		this.gen = new ExponentialGenerator(requestPerSecond, rng);	
	
		this.executor = executor;
		
		this.run();
		
	}
	
	
	
		public void run() {
			long millisecAtStart = System.currentTimeMillis();
			int i =0;
			while ((millisecAtStart+millisecPerClass) >=System.currentTimeMillis())
			{
			    long interval = Math.round(gen.nextValue() * ONE_SECOND);
			    try {
			    	// System.out.println(interval);
			    	 
					Thread.sleep(interval);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			    //System.out.println(new Date());
			    Thread exThread = new Thread(executor); 
			    exThread.start();
			}

	}

}
